package com.united.ucts.modules.juc.lock;

/**
 * 演示死锁样例
 *
 * @author hudan
 * @version 1.0
 * @filename DeadLockDemo.java
 * @copyright <a href="www.chancein.com">www.chancein.com</a>
 * @date 2022-10-18
 */
public class DeadLockDemo {

    // 创建两个类成员变量,用于对象间的死锁测四
    private static Object obj1 = new Object();
    private static Object obj2 = new Object();

    public static void main(String[] args) {
        new Thread(() -> {
            // 当前线程需要处理的业务逻辑
            synchronized (obj1) {
                System.out.println(Thread.currentThread().getName() + ", 只有当前锁:A,试图获取锁B!");

                synchronized (obj2) {
                    System.out.println(Thread.currentThread().getName() + ",获取锁B!");

                }
            }
        }, "A").start();


        new Thread(() -> {
            // 当前线程需要处理的业务逻辑
            synchronized (obj2) {
                System.out.println(Thread.currentThread().getName() + ", 只有当前锁:B,试图获取锁A!");

                synchronized (obj1) {
                    System.out.println(Thread.currentThread().getName() + ",获取锁A!");

                }
            }
        }, "B").start();
    }
}